package com.stbu.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stbu.bean.Employments;
import com.stbu.mapper.EmploymentMapper;
import com.stbu.models.EmploymentModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
@RequiredArgsConstructor
public class EmploymentService extends ServiceImpl<EmploymentMapper, Employments> {
    private final EmploymentMapper employmentMapper;


    public Page<Employments> getLists(EmploymentModel param) {
        Employments emp = new Employments();
        emp.setEid(param.getEid());
        emp.setEage(param.getEage());
        emp.setEaddr(param.getEaddr());
        emp.setIdcard(param.getIdcard());
        emp.setEname(param.getEname());
        emp.setSex(param.getSex());
        QueryWrapper<Employments> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(emp);
        Page page = new Page();
        page.setCurrent(param.getCurrent());
        page.setSize(param.getSize());
        Page<Employments> employmentsPage = employmentMapper.selectPage(page,queryWrapper);
        return employmentsPage;
    }

    public String addEmployment(EmploymentModel addModel) {
        Employments emp = new Employments();
        emp.setEname(addModel.getEname());
        emp.setSex(addModel.getSex());
        emp.setEage(addModel.getEage());
        emp.setIdcard(addModel.getIdcard());
        emp.setEaddr(addModel.getEaddr());
        try {
            employmentMapper.insert(emp);
        } catch (Exception e) {
            e.printStackTrace();
            return "新增操作不成功!";
        }

        return null;
    }

    public String updateEmployment(EmploymentModel updateModel) {
        Integer eid = updateModel.getEid();
        if( eid == null) {
            return "Id不能为空!";
        } else {
            Employments emp = new Employments();
            emp.setEid(eid);
            if(updateModel.getEname() !=null && updateModel.getEname().length() !=0) {
                emp.setEname(updateModel.getEname());
            }
            if (updateModel.getEage() !=null) {
                emp.setEage(updateModel.getEage());
            }
            if (updateModel.getIdcard() !=null && updateModel.getIdcard().length() !=0) {
                emp.setIdcard(updateModel.getIdcard());
            }
            if (updateModel.getEaddr() !=null && updateModel.getEaddr().length() !=0) {
                emp.setEaddr(updateModel.getEaddr());
            }
            employmentMapper.updateById(emp);
            return null;
        }
    }

    private boolean delEmployById(Serializable id) {
        return super.removeById(id);
    }


}
